<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once APPPATH . 'controllers/main.php';

/**
 * Контроллер просмотра новостей
 *
 * @package CoreFramework
 * @version 1.0
 * @author Gennadiy Kozlenko
 */
class News extends Main {
   
   /**
    * Номер страницы
    *
    * @access private
    * @var integer
    */
   var $_page = 1;
   
   /**
    * Количество результатов на страницу
    *
    * @access private
    * @var integer
    */
   var $_per_page = 10;
   
   /**
    * Массив найденых результатов
    *
    * @access private
    * @var array
    */
   var $_results = array();
   
   /**
    * Общее количество результатов поиска
    *
    * @access private
    * @var integer
    */
   var $_all_count = 0;
   
   /**
    * Конструктор класса
    *
    * @access public
    * @return News
    */
   function News() {
      parent::Main();
      if (false !== $this->input->get('page')) {
         $this->_page = (int) $this->input->get('page');
         $this->_page = 1 < $this->_page ? $this->_page : 1;
      }
      $this->_per_page = $this->variables->get('news_per_page');
   } // end News
   
   /**
    * Метод по умолчанию
    *
    * @access public
    */
   function index() {
      $pagination = '';
      // Подгружаем результаты
      $this->_load_results();
      if ($this->_all_count > count($this->_results)) {
         $this->load->helper('link');
         $this->load->library('pagination');
         $this->pagination->set_page($this->_page);
         $this->pagination->set_num_pages(ceil($this->_all_count / $this->_per_page));
         $this->pagination->set_url(create_link($this->get_controller(), $this->get_method(), array('page' => '{%page%}')));
         $pagination = $this->pagination->create_links();
      }
         
      // path
      $links = array(
         array(
            'title' => $this->lang->line('front_title'),
            'link'  => base_url()
         ),
         array(
            'title' => $this->lang->line('news_title'),
            'link'  => ''
         )
      );
      $this->load->helper('path');
      $this->add_param('path', create_path($links));
      $this->add_param('link', create_link($this->get_controller(), $this->get_method()));
      
      $this->add_param('title', $this->lang->line('news_title'));
            
      $data = array(
         'results' => $this->_results,
         'pagination' => $pagination
      );
      $this->set_content($this->parser->parse('news/body.html', $data, true));
      $this->display();
   } // end index
   
   /**
    * Процесс поиска
    *
    * @access private
    */
   function _load_results() {
      $this->load->model('m_select_news');
      $this->m_select_news->set_limit($this->_per_page, ($this->_page - 1) * $this->_per_page);
      $this->m_select_news->load_results();
      $this->_results = $this->m_select_news->get_results();
      $this->_all_count = $this->m_select_news->get_all_count();
   } // end load_results
   
}
